﻿using System.Collections.Generic;
using UnityEngine;

[CreateAssetMenu]
public class Atlas : ScriptableObject
{
    //[ContextMenuItem("Test", "aa")]
    public List<Sprite> spriteList = new List<Sprite>();
    private Dictionary<string, Sprite> _spriteDict;

    public Sprite this[string name]
    {
        get
        {
            return GetSprite(name);
        }
    }

    public Sprite GetSprite(string spriteName)
    {
        if (null == _spriteDict)
        {
            _spriteDict = new Dictionary<string, Sprite>();
            foreach (var sprite in spriteList)
                _spriteDict[sprite.name] = sprite;
        }

        if (!_spriteDict.TryGetValue(spriteName, out var sp))
            Debug.LogError($"sprite not found: {spriteName} in atlas: {name}");

        return sp;
    }

}
